<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <!-- logo黑马 -->
        <div class="left">
          <img src="../assets/heima.b5a855ee.png" alt />
          电商后台管理系统
        </div>
        <!-- 退出按钮 -->
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏菜单区 -->
        <div class="toggle_btn" @click="isCollapse = !isCollapse">| | |</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="skyblue"
          unique-opened
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savePath('/'+subItem.path)"
            >
              <span class="el-icon-menu"></span>
              {{subItem.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 激活的菜单
      activePath: "",
      // 控制菜单展开折叠布尔值
      isCollapse: false,
    };
  },
  created() {
    // 在钩子函数中调用获取左侧数据方法
    this.getMenuList();
    // console.log( sessionStorage.getItem('path'));
    this.activePath = sessionStorage.getItem("path");
  },
  methods: {
    logout() {
      // 1.跳转到login页面
      this.$router.push("/login");
      // 2.清除token
      sessionStorage.clear();
      // 3.退出成功提示
      this.$message.success("退出成功");
      // 到此为止完成率登录退出的功能
      // 1. git status 检查文件状态
      // 2. git add .
      // 3. git commit -m 备注信息
      // 4. git checkout master (切回主分支)
      // 5. git merge login(分支名)  合并的时候一定是主分支去合并分分支
      // 6. git push
      // 7. 推送分支 git push -u origin login(分支名)
    },
    // 获取左侧菜单列表数据
    async getMenuList() {
      const { data: res } = await this.$http("menus");
      if (res.meta.status !== 200)
        return this.$message.error("获取数据失败,请稍后重试");
      console.log(res);
      this.menulist = res.data;
    },
    // 修改激活路径
    savePath(path) {
      console.log(path);
      this.activePath = path;
      // 将路径保存到本地存储中
      sessionStorage.setItem("path", path);
    },
  },
};
</script>

<style lang="less" scope>
.home_container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    padding-left: 0;
    > div {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .left {
      font-size: 20px;
      color: #fff;
      img {
        vertical-align: middle;
        margin-right: 20px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .iconfont {
      margin-right: 10px;
    }
    .toggle_btn {
      color: #fff;
      text-align: center;
      background-color: #373d41;
      cursor: pointer;
    }
    .el-menu {
      border-right: 0;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>